# -*- coding: utf-8 -*-
'''
Created on 2011-10-6

@author: y
'''

from django.template import TemplateSyntaxError, Node, Variable, Library, resolve_variable, VariableDoesNotExist
from django import template
#from django.utils.html import escape

def do_paging_nav(parser, token):
    #用于输出分页导航块。
    sections = token.split_contents()
    if len(sections) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument (path to a view)" % sections[0])
    sections = sections[1:]
    return PagingNavBlock(sections)

class PagingNavBlock(Node):
    
    def __init__(self,sections):
        '''
        Constructor
        '''
        self.sections=sections;
    
    def render(self, context):
        url = resolve_variable(self.sections[0],context)#"abc?pn=%d"#self.sections[0]
        pn = resolve_variable(self.sections[1],context)
        pc = resolve_variable(self.sections[2],context)

        html=""
        if pn > pc:
            pn = pc
        if pc==1:
            html="<a title=\"上一页\"><span>&#8249;&#8249;上一页</span></a><b><span title='第一页'>1</span></b><a title='下一页'><span>下一页&#8250;&#8250;</span></a>"
        else:
            if pn==1:
                html=("<a title='上一页' href='%s'><span>&#8249;&#8249;上一页</span></a><b><span title='第1页'>1</span></b>" % ( url % pn))
            else:
                html=("<a title='上一页' href='%s'><span>&#8249;&#8249;上一页</span></a><a title='第1页' href='%s'><span>1</span></a>" % (url % (pn-1),(url % 1)))
            
            start=end=0
            if pc < 10:
                start = 2
                end = pc -1
            elif pn < 5:
                start = 2
                end = 8
            elif pn > (pc-4):
                start = pc-7
                end = pc -1
            else:
                start=pn-3
                end=pn+3
            
            if start > 2:
                html = "%s<span>...</span>" % (html)
            
            for index in range(start,end):
                if index==pn:
                    html = "%s<b><span title='第%d页'>%d</span></b>" % (html, index, index)
                else:
                    html = "%s<a title='第%d页' href='%s'><span>%d</span></a>" % (html, index, (url % index), index)
                    
            
            if end < (pc-1):
                html = "%s<span>...</span>" % (html)
            
            if pc > 1 and pn == pc:
                html = "%s<b><span title='第%d页'>%d</span></b>" % (html, pn, pn)
            elif pc > 1:
                html = "%s<a title='第%d页' href='%s'><span>%d</span></a>" % (html, pc, (url % pc), pc)
            
            if pn == pc:
                html = "%s<a title='下一页' href='%s'><span>下一页&#8250;&#8250;</span></a>" % (html, (url % pc))
            else:
                html="%s<a title='下一页' href='%s'><span>下一页&#8250;&#8250;</span></a>" % (html, (url % (pn+1)))
            
        html = "%s<span class='left'>跳页</span><input type=\"text\" class=\"input\" value=\"%d\" id=\"txtJumpPage_pn\"\" title=\"请输入页码\"/><button title=\"Go\" class=\"gopage\" onclick=\"sendActionWithPageNo('%s', 'txtJumpPage_pn')\"></button>" % (html, pn, url)
        return html
    
register = template.Library()
register.tag("paging_nav", do_paging_nav)